﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;

namespace IHISCETeim.DAL
{
    public class YYShipmentDAL
    {
        public static int Add(List<YYShipmentInfo> list)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            try
            {
                foreach (YYShipmentInfo obj in list)
                {
                    DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_Add");
                  


                    //DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                    //DB.AddInParameter(dbCommand, "MacType", DbType.String, obj.MacType);
                    //DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    //DB.AddInParameter(dbCommand, "ShippingType", DbType.String, obj.ShippingType);
                    //DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                    //DB.AddInParameter(dbCommand, "Status", DbType.String, obj.Status);
                    

                    DB.AddInParameter(dbCommand, "OrderDate", DbType.String, obj.OrderDate);
                    SqlParameter ExpectOutDate = new SqlParameter("@ExpectOutDate", SqlDbType.DateTime);
                    ExpectOutDate.IsNullable = true;
                    if (obj.ExpectOutDate == DateTime.MinValue)
                        ExpectOutDate.Value = DBNull.Value;
                    else
                        ExpectOutDate.Value = obj.ExpectOutDate;
                    dbCommand.Parameters.Add(ExpectOutDate);
                    DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                    DB.AddInParameter(dbCommand, "MacType", DbType.String, obj.MacType);
                    DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    DB.AddInParameter(dbCommand, "ReadyDate", DbType.String, obj.ReadyDate);
                    DB.AddInParameter(dbCommand, "OutDate", DbType.String, obj.OutDate);
                    DB.AddInParameter(dbCommand, "ShippingCmpt", DbType.String, obj.ShippingCmpt);
                    DB.AddInParameter(dbCommand, "ShippingType", DbType.String, obj.ShippingType);
                    DB.AddInParameter(dbCommand, "ShippingClass", DbType.String, obj.ShippingClass);
                    DB.AddInParameter(dbCommand, "IsInsure", DbType.String, obj.IsInsure);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                    DB.AddInParameter(dbCommand, "Status", DbType.String, obj.Status);

                    DB.ExecuteScalar(dbCommand);
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }


        public static bool Delete(int ShipmentID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_Delete", ShipmentID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetByID(int ShipmentID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_GetByID", ShipmentID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }

        public static bool Update(YYShipmentInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

           // DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_Update", ShipmentID, ExpectOutDate, AgentName, MacType, SerialNumber, ShippingType, Remark, Status);

            DbCommand dbCommand = DB.GetStoredProcCommand("YYShipment_Update");
           

            try
            {
                SqlParameter ExpectOutDate = new SqlParameter("@ExpectOutDate", SqlDbType.DateTime);
              

                DB.AddInParameter(dbCommand, "ShipmentID", DbType.Int32, obj.ShipmentID);
                DB.AddInParameter(dbCommand, "OrderDate", DbType.String, obj.OrderDate);
                ExpectOutDate.IsNullable = true;
                if (obj.ExpectOutDate == DateTime.MinValue)
                    ExpectOutDate.Value = DBNull.Value;
                else
                    ExpectOutDate.Value = obj.ExpectOutDate;
                dbCommand.Parameters.Add(ExpectOutDate);
                DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                DB.AddInParameter(dbCommand, "MacType", DbType.String, obj.MacType);
                DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                DB.AddInParameter(dbCommand, "ReadyDate", DbType.String, obj.ReadyDate);
                DB.AddInParameter(dbCommand, "OutDate", DbType.String, obj.OutDate);
                DB.AddInParameter(dbCommand, "ShippingCmpt", DbType.String, obj.ShippingCmpt);
                DB.AddInParameter(dbCommand, "ShippingType", DbType.String, obj.ShippingType);
                DB.AddInParameter(dbCommand, "ShippingClass", DbType.String, obj.ShippingClass);
                DB.AddInParameter(dbCommand, "IsInsure", DbType.String, obj.IsInsure);
                DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                DB.AddInParameter(dbCommand, "Status", DbType.String, obj.Status);

                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
